// JavaScript source code
//点击按钮搜索
function search() {
    var key = document.getElementById("inp12138").value;
    var wd = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + key;
    window.open(wd);
}
//回车搜索
function hcbd() {
    var event = window.event || arguments.callee.caller.arguments[0];
    if (event.keyCode == 13) {
        search();
    }
}
//加载到div
function autoLoad(obj) {
    
    var style = document.createElement('style');
    document.head.appendChild(style);
    sheet = style.sheet;
    sheet.addRule('.tip:hover', 'background-color:rgb(238,238,242)');

    var parent = document.getElementById(obj);
    var left = document.createElement("div");
    left.setAttribute("style", "width:70%;height:100%;float:left");
    var right = document.createElement("div");
    right.setAttribute("style", "width:25%;height:100%;float:right;margin-top:1px;");
    var bot = document.createElement("input");
    bot.setAttribute("type", "button");
    bot.setAttribute("style","height:100%;width:100%;")
    bot.value = "百度搜索";
    bot.setAttribute("onclick","search()");
    right.appendChild(bot);
    parent.appendChild(left);
    parent.appendChild(right);
    var inp = document.createElement("input");
    var ul = document.createElement("ul");
    var sugurl = "";
    var scp = document.createElement("script");
    scp.src = sugurl;
    ul.setAttribute("style", "width:100%;height:100%;margin:0 0 0 0;padding:0 2px 0 2px");
    inp.setAttribute("value","");
    inp.setAttribute("style", "width:100%;height:100%;border:1px solid;border-radius:3px;padding:0 0 0 1px;");
    inp.setAttribute("id", "inp12138")
    inp.setAttribute("onkeydown", "hcbd()");
    left.appendChild(scp);
    left.appendChild(inp);
    left.appendChild(ul);
    //定义回调方法
    window.baidu = {
        sug: function (json) {
            var arr = json.s;
            left.removeChild(ul);
            ul = document.createElement("ul");
            ul.setAttribute("style", "width:100%;height:100%;margin:0 0 0 0;padding:0 2px 0 2px");
            var tip;
            var a;
            for (var i = 0; i < arr.length; i++) {
                tip = document.createElement("li");
                a = document.createElement("a");
                a.href = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=' + arr[i];
                a.innerHTML =arr[i];
                a.setAttribute("style", "text-decoration:none;color:black")
                a.setAttribute("target","_BLANK")
                tip.appendChild(a);
                tip.setAttribute("class","tip")
                ul.appendChild(tip)
            }
            left.appendChild(ul);
        }
    }
    //获取输入提示
    getTip(left, inp, scp); 
}
//获取输入提示(百度api)
function getTip(parent, inp, scp) {
    parent.removeChild(scp);
    var key = inp.value;
    var nscp = document.createElement("script");
    if (key != "") {
        nscp.src = "https://suggestion.baidu.com/su?wd=" + key + "&cb=window.baidu.sug";
    } else {
        nscp.src = "";
    }
    parent.appendChild(nscp);  
    setTimeout(getTip, 2000, parent, inp, nscp);
}